# -*- mode: python; coding: utf-8; -*-
##--------------------------------------------------------------------------##
##
## Copyright (C) 1998-2003 Markus Oberhumer <markus@oberhumer.com>
## Copyright (C) 2003 Mt. Hood Playing Card Co.
## Copyright (C) 2005-2010 Skomoroh <skomoroh@users.sourceforge.net>
## Copyright (C) 2010-2013 Jeremy Austin-Bardo <tjaustinbardo@gmail.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##--------------------------------------------------------------------------##
__docformat__ = "restructuredtext en"
__all__ = []

from ... import pilelib as stack
from ... import stackfill as fill
from ...forms import canfield as clayout
from ...gamelib import info as gi, state, game
from ...packlib import info as cs
from ...piles import tarock as tstack
from ...utillib.misc import kwdefault

Wrap = stack.StackWrapper
GT, SL = gi.GT, gi.SL


class Grasshopper(cs.Tarock_Deck, game.Game):
    """
    Finish 1 deck on _foundation_.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.layouts.canfield.CanfieldLayout
        - row [6 horz S foundation, vert offset]
        - waste [1 E talon, no offset]
        - reserve [1 S talon, vert offset]
        - foundation [5 horz N, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.WasteTalon (max_rounds=2)
        Deal 1 card per waste on click. 2 redeals.

      _waste_ : pysollib.stack.Stack
        .. pysollib.stack.WasteStack
        Init 1 card. Build none. Play top.

      _foundation_ : pysollib.stack.Stack
        .. stack.SS_Foundation
        Build up in rank by same suit. Play top. Base ace or low trump.

      _row_ : pysollib.stack.Stack
        .. pysollib.stacks.tarock.TrumpOnly_RowStack,
          pysollib.stacks.tarock.TrumpAC_RowStack
        W 1: Init 1 card. Build down in rank alone by trump. Play sequence.
          Base trump.
        W 5: Init 1 card. Build down in rank by alt color and trump by rank
          alone. Play sequence. Gap deal 1 card from reserve.

      _reserves_ : pysollib.stack.Stack
        .. pysollib.stack.ReserveStack
        Build any. Play top. Max 1 card.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.CautiousDefaultHint
    """
    __info__ = dict(id=163, name="Grasshopper", category=cs.TYPE_TAROCK,
                    game_type=GT.CANFIELD, decks=1, redeals=1,
                    skill_level=SL.MOSTLY_SKILL, version='4.00')
    __author__ = 'Markus Oberhumer'
    __version__ = 7.70

    _layout_ = clayout.CanfieldLayout
    _talon_ = Wrap(stack.WasteTalon, max_rounds=2)
    _waste_ = stack.WasteStack
    _foundation_ = stack.SS_Foundation
    _rows_ = (tstack.TrumpOnly_RowStack, tstack.TrumpAC_RowStack,)
    _reserve_= stack.OpenStack
    __VERSION__ = 7.70

    def _create_game(self, trump=True, **kw):
        l, s = self._layout_(self, trump=trump), self.s
        kwdefault(kw, rows=6, playcards=15, texts=True)
        l.create(**kw)
        self.table_size = l.size
        s['talon'] = self._talon_(l.s['talon'].x, l.s['talon'].y, game=self)
        for pile in l.s['wastes']:
            s['wastes'].append(self._waste_(r.x, r.y, game=self))
        for pile in l.s['foundations']:
            s['foundations'].append(self._foundation_(pile.x, pile.y, game=self,
                                                   suit=pile.suit))
        for pile in l.s['reserves']:
            s['reserves'].append(self._reserve_(r.x, r.y, game=self,
                                                offset=r.offset))
        for pile in l.s['rows'][:l.NDECKS]:
            s['rows'].append(self._rows_[0](pile.x, pile.y, game=self))
        for pile in l.s['rows'][l.NDECKS:]:
            s['rows'].append(self._rows_[1](pile.x, pile.y, game=self))
        l.defaultAll()
        return l

    def _start(self, **kw):
        """
        Deal initial cards for new game.
          - s[reserves] RANKS * DECKS ea top up.
          - s[rows][:DECKS] 1 ea.

        .. \_start([row=5, flip=False]) -> None
        """
        for i in range(self['decks'] * len(self['ranks'] - 1)):
            self.s['talon'].dealRow(rows=self.s['reserves'], flip=False)
        self.s['talon'].dealRow(rows=self.s['reserves'])
        self.s['talon'].dealRow(rows=self.s['rows'][self['decks']:])

    @state.move_state('fill')
    def _auto_fill(self, stack):
        fill.fromReserve(self, stack, self.s['rows'][self['decks']:])


class DoubleGrasshopper(Grasshopper):
    """
    Finish 2 deck on _foundation_.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.layouts.canfield.CanfieldLayout
        - row [11 horz S foundation, vert offset]
        - waste [1 E talon, no offset]
        - reserve [1 S talon, vert offset]
        - foundation [5 horz N, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.WasteTalon (max_rounds=2)
        Deal 1 card per waste on click. 2 redeals.

      _waste_ : pysollib.stack.Stack
        .. pysollib.stack.WasteStack
        Init 1 card. Build none. Play top.

      _foundation_ : pysollib.stack.Stack
        .. stack.SS_Foundation
        Build up in rank by same suit. Play top. Base ace or low trump.

      _row_ : pysollib.stack.Stack
        .. pysollib.stacks.tarock.TrumpOnly_RowStack,
          pysollib.stacks.tarock.TrumpAC_RowStack
        W 2: Init 1 card. Build down in rank alone by trump. Play sequence.
          Base trump.
        W 9: Init 1 card. Build down in rank by alt color and trump by rank
          alone. Play sequence. Gap deal 1 card from reserve.

      _reserves_ : pysollib.stack.Stack
        .. pysollib.stack.ReserveStack
        Build any. Play top. Max 1 card.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.CautiousDefaultHint
    """
    __info__ = dict(id=164, name="Double Grasshopper", decks=2,
                    category=cs.TYPE_TAROCK, game_type=GT.CANFIELD, redeals=1,
                    skill_level=SL.MOSTLY_SKILL, version='4.00')
    __author__ = 'Markus Oberhumer'
    __version__ = 7.70

    def _create_game(self, **kw):
        kw.default('rows', 11)
        return super(DoubleGrasshopper, self)._create_game(**kw)


class Cockroach(Grasshopper):
    """
    Finish 1 deck on _foundation_.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.layouts.canfield.CanfieldLayout
        - row [6 horz S foundation, vert offset]
        - waste [1 E talon, no offset]
        - reserve [1 S talon, vert offset]
        - foundation [5 horz N, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.WasteTalon
        Deal 1 card per waste on click. No redeals.

      _waste_ : pysollib.stack.Stack
        .. pysollib.stack.WasteStack
        Init 1 card. Build none. Play top.

      _foundation_ : pysollib.stack.Stack
        .. stack.SS_Foundation
        Build up in rank by same suit. Play top. Base ace or low trump.

      _row_ : pysollib.stack.Stack
        .. pysollib.stacks.tarock.TrumpOnly_RowStack,
          pysollib.stacks.tarock.TrumpAC_RowStack
        W 1: Init 1 card. Build down in rank alone by trump. Play sequence.
          Base trump.
        W 5: Init 1 card. Build down in rank by alt color and trump by rank
          alone. Play sequence. Gap deal 1 card from reserve.

      _reserves_ : pysollib.stack.Stack
        .. pysollib.stack.ReserveStack
        Build any. Play top. Max 1 card.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.CautiousDefaultHint
    """
    __info__ = dict(id=13163, name='Cockroach', category=cs.TYPE_TAROCK,
                    game_type=GT.CANFIELD, decks=1,
                    skill_level=SL.MOSTLY_SKILL, version='0.50fc')
    __author__ = 'Mt. Hood Playing Card'
    __version__ = 7.70

    _talon_ = stack.WasteTalon


class DoubleCockroach(DoubleGrasshopper):
    """
    Finish 2 deck on _foundation_.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        .. pysollib.layouts.canfield.CanfieldLayout
        - row [11 horz S foundation, vert offset]
        - waste [1 E talon, no offset]
        - reserve [1 S talon, vert offset]
        - foundation [5 horz N, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.WasteTalon
        Deal 1 card per waste on click. No redeals.

      _waste_ : pysollib.stack.Stack
        .. pysollib.stack.WasteStack
        Init 1 card. Build none. Play top.

      _foundation_ : pysollib.stack.Stack
        .. stack.SS_Foundation
        Build up in rank by same suit. Play top. Base ace or low trump.

      _row_ : pysollib.stack.Stack
        .. pysollib.stacks.tarock.TrumpOnly_RowStack,
          pysollib.stacks.tarock.TrumpAC_RowStack
        W 2: Init 1 card. Build down in rank alone by trump. Play sequence.
          Base trump.
        W 9: Init 1 card. Build down in rank by alt color and trump by rank
          alone. Play sequence. Gap deal 1 card from reserve.

      _reserves_ : pysollib.stack.Stack
        .. pysollib.stack.ReserveStack
        Build any. Play top. Max 1 card.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.CautiousDefaultHint
    """
    __info__ = dict(id=13164, name='Double Cockroach',
                    category=cs.TYPE_TAROCK, game_type=GT.CANFIELD, decks=2,
                    skill_level=SL.MOSTLY_SKILL, version='0.50fc')
    __author__ = 'Mt. Hood Playing Card'
    __version__ = 7.70

    _talon_ = stack.WasteTalon


PROVIDES = [Grasshopper, DoubleGrasshopper, Cockroach, DoubleCockroach]